package com.px.baolaiapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.px.mbg.mapper.StandardDeliveryDetailRecordMapper;
import com.px.mbg.model.StandardDelivery;
import com.px.mbg.model.StandardDeliveryDetail;
import com.px.mbg.model.StandardDeliveryDetailRecord;
import com.px.baolaiapi.dto.WhChangeDto;
import com.px.baolaiapi.service.IStandardDeliveryDetailRecordService;
import com.px.baolaiapi.service.IStandardDeliveryDetailService;
import com.px.baolaiapi.service.IStandardDeliveryService;
import com.px.baolaiapi.service.IWhOfStorageLocationService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 标准出货单明细记录表 服务实现类
 * </p>
 *
 * @author 品讯科技
 * @since 2022-03-24
 */
@Service
public class StandardDeliveryDetailRecordServiceImpl extends ServiceImpl<StandardDeliveryDetailRecordMapper, StandardDeliveryDetailRecord> implements IStandardDeliveryDetailRecordService {

    @Autowired
    private IStandardDeliveryDetailService standardDeliveryDetailService;
    @Autowired
    private IStandardDeliveryService standardDeliveryService;
    @Autowired
    private IWhOfStorageLocationService whOfStorageLocationService;

    @Override
    @Transactional
    public void addStandardDeliveryDetailRecord(StandardDeliveryDetailRecord[] standardDeliveryDetailRecords) {
        if(ArrayUtils.isEmpty(standardDeliveryDetailRecords)){
            return;
        }
        for (StandardDeliveryDetailRecord standardDeliveryDetailRecord : standardDeliveryDetailRecords) {
            this.save(standardDeliveryDetailRecord);
            // 出库
            StandardDeliveryDetail standardDeliveryDetail = standardDeliveryDetailService.getById(standardDeliveryDetailRecord.getStandardDeliveryDetailId());
            StandardDelivery standardDelivery = standardDeliveryService.getById(standardDeliveryDetail.getStandardDeliveryId());
            WhChangeDto whChangeDto = new WhChangeDto();
            whChangeDto.setStorageId(standardDeliveryDetailRecord.getStorageId());
            whChangeDto.setStgeLocCode(standardDeliveryDetailRecord.getStorageLocationCode());
            whChangeDto.setMaterialCode(standardDeliveryDetail.getMaterialCode());
            whChangeDto.setMaterialName(standardDeliveryDetail.getMaterialName());
            whChangeDto.setCount(standardDeliveryDetailRecord.getTakeCount());
            whChangeDto.setBnoNo(standardDelivery.getBnoNo());
            whChangeDto.setReceiptsType(standardDelivery.getReceiptsType());
            whOfStorageLocationService.takeOutStorage(whChangeDto);
        }
    }
}
